/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：FunNodeDaoImpl.java
 * History:
 *       2012-1-13: Initially created, tangkf.
 */
package com.xteam.purview.service.impl;

import java.util.List;

import com.tangkf.utils.MDFiveEncrypt;
import com.xteam.db.IBaseDao;
import com.xteam.purview.entities.PurviewUser;
import com.xteam.purview.entities.PurviewUserFunDatarule;
import com.xteam.purview.entities.PurviewUserRole;
import com.xteam.purview.service.AbsFace;
import com.xteam.purview.service.IPurviewUserFace;

/**
 * @author tangkf ---2012-1-13
 */
public class PurviewUserFace extends AbsFace<PurviewUser> implements IPurviewUserFace {
	
	private PurviewUserFace() {
	}

	private static PurviewUserFace	instance	= new PurviewUserFace();

	public static PurviewUserFace getInstance() {
		return instance;
	}
	
	public static final String superMan	= "superman";   // 超级管理员名称，不存入数据库，只在程序中标识。
	public static final String superPwd	= "xuminginfo"; // 超级管理员密码。

	/**
	 * @see com.xteam.purview.service.AbsFace#save(java.lang.Object)
	 */
	public int save(PurviewUser obj) {
		String pwd	= obj.getPassword();
		pwd	= MDFiveEncrypt.getMD5Encode(pwd);
		obj.setPassword(pwd);
		return super.save(obj);
	}

	/**
	 * @see com.xteam.purview.service.AbsFace#update(java.lang.Object)
	 */
	public int update(PurviewUser obj) {
		String newPwd	= obj.getPassword();
		//小于17位，说明用户更改了密码
		if(newPwd.length()<17) obj.setPassword(MDFiveEncrypt.getMD5Encode(newPwd));
		return super.update(obj);
	}


	/**
	 * MD5加密密码方式
	 * @author tangkf  ---2012-2-8
	 * @param userId
	 * @param password
	 * @return
	 */
	public PurviewUser getUser(String userId, String password) {
		
		if(superMan.equalsIgnoreCase((userId)) && superPwd.equalsIgnoreCase(password)){
			PurviewUser pu	= new PurviewUser();
			pu.setUserId(superMan);
			pu.setUname(superMan);
			pu.setIssys("1");
			pu.setNickName(superMan);
			return pu;
		}
		
		String md5pwd	= MDFiveEncrypt.getMD5Encode(password);//加密
		String hql = IBaseDao.SQL_FROM + PurviewUser.REF + IBaseDao.SQL_WHERE + 
		PurviewUser.PROP_UNAME + IBaseDao.SQL_EQUAL + "?" + IBaseDao.SQL_AND
				+ PurviewUser.PROP_PASSWORD + IBaseDao.SQL_EQUAL + "?";
		List<PurviewUser> users = null;
		try {
			users = dao.queryAll(hql, new String[] { userId, md5pwd });
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (users != null && users.size() > 0) ? users.get(0) : null;
	}

	/**
	 * 覆盖父类方法，增加了清空用户与功能，角色的关系
	 * @see com.xteam.purview.service.AbsFace#delBatch(java.lang.Class, java.lang.String, java.lang.String[])
	 */
	public int delBatch(Class<PurviewUser> clazz, String idName, String[] ids) {
		int r = 0;
		// 清空用户与功能，角色的关系
		try {
			r = dao.deleteBathById(PurviewUserFunDatarule.class, PurviewUserFunDatarule.PROP_USERID, ids);
			r = dao.deleteBathById(PurviewUserRole.class, PurviewUserRole.PROP_USERID, ids);
		} catch (Exception e) {
			r = 1;
			e.printStackTrace();
		}
		r = super.delBatch(clazz, idName, ids);
		return r;
	}

	/**
	 * 返回属于某个角色的用户列表
	 * @author tangkf ---2012-1-18
	 * @param roleId
	 * @return
	 */
	public List<PurviewUser> getUserListByRoleId(String roleId) {
		// TODO
		return null;
	}

	/**
	 * 返回对指定功能节点有权限的用户列表
	 * @author tangkf ---2012-1-18
	 * @param funNodeId
	 * @return
	 */
	public List<PurviewUser> getUserListByFunNodeId(String funNodeId) {
		// TODO
		return null;
	}
	
	/**
	 * 根据用户ID获得用户角色关系表数据
	 * @see com.xteam.purview.service.IPurviewUserFace#getPurUserRoleByUserId(java.lang.String)
	 */
	public PurviewUserRole getPurUserRoleByUserId(String userId){
		try {
			String hql = "from " + PurviewUserRole.REF + " where userId = ?";
			
			List<PurviewUserRole> userRoleList = dao.queryAll(hql, new Object[]{userId});
			
			if (userRoleList != null && !userRoleList.isEmpty()) {
				return userRoleList.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
